// pages/topicality/bsy/bsy.js
const app = getApp()
const util = require("../../../utils/util.js");
var banIndex = [];
var showorhide = [];
var showTap = [];
var conditions, condition;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // banIndex: 0,
        showNav: true, // 快捷导航是否展开
        showtk: false, // 是否显示外链客服弹框
        collectCue: false,
        infoData: [],
        wzList: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        this.setData({
            id: options.id ? options.id : "",
            citycode: options.city ? options.city : (wx.getStorageSync("citycode") ? wx.getStorageSync("citycode") : 'sz'),
        })

        util.getUrl(app.globalData.url).then((res) => {
            util.cityList().then((res) => {
                let citydata = res.allcity.filter(v => v.citycode == this.data.citycode);
                if (citydata[0])
                    this.setData({
                        city: Array.isArray(citydata) ? citydata[0].cityname : wx.getStorageSync("city")
                    })
            })
            // 获取城市公众号二维码
            util.cityConfig(this.data.citycode).then(res => {
                if (res)
                    this.setData({
                        qrcodeimg: res.qrcode,
                    })
            })
        })
    },

    onShow() {
        util.getUrl(app.globalData.url).then((res) => {
            this.getbanshiData();
            this.isCollect(this.data.id);
        })
    },

    // 获取办事指南接口
    getbanshiData() {
        let data = {
            citycode: this.data.citycode == 'sz' ? 'bsy' : this.data.citycode,
            action: 'zt',
            id: this.data.id
        };
        util.request('index.php', data, 'GET').then(res => {
            conditions = res.data.conditions;
            condition = res.data.condition;
            if (conditions)
                for (let i = 0; i < conditions.length; i++) {
                    banIndex[i] = 0;
                    showorhide[i] = [];
                    for (let j = 0; j < conditions[i].content.length; j++) {
                        showorhide[i][j] = true;
                    }
                }
            for (let i = 0; i < showorhide.length; i++) {
                if (showorhide[i].indexOf(true) != -1) {
                    showTap.push(true)
                } else {
                    showTap.push(false)
                }

            }
            this.setData({
                banIndex,
                bsyData: res.data,
                showTap,
                collectValue: {
                    "id": this.data.id,
                    "title": res.data.title,
                    "path": "/pages/topicality/bsy/bsy"
                }
            })
            let e = {
                currentTarget: {
                    dataset: {
                        n: 0,
                        m: 0
                    }
                }
            };
            this.banliTap(e);
            // this.getInfoData();
            this.getwzList(res.data.classid)
        })
    },

    // 获取办事内容接口
    getInfoData(bsyid) {
        let data = {
            citycode: this.data.citycode == 'sz' ? 'bsy' : this.data.citycode,
            action: 'bsyhtml',
            id: bsyid,
        };
        util.request('index.php', data, 'GET').then(res => {
            this.setData({
                content: res.data.content[0].content,
                infoData: res.data,
                linkData: res.data.content[0].links,
            })
        })
    },
    // 富文本内容可点击的标签
    linkTap(e) {
        let url = e.detail.href;
        if (this.data.linkData != '') {
            let urlRes = [];
            urlRes = this.data.linkData.filter((v, k) => {
                var str = v.url;
                str = str.replace(/&amp;/g, "&");
                if (str == url) {
                    return v
                }
                // if (v.id != 0 && v.url == url) {
                //   return v
                // }
            })
            if (urlRes != '') {
                let id = urlRes[0].id;
                let type = urlRes[0].type;
                let citycode = urlRes[0].city ? urlRes[0].city : this.data.citycode;
                if (id != 0) {
                    wx.navigateTo({
                        url: '/pages/article/detail/detail?id=' + id + '&type=' + type + '&city=' + citycode,
                    });
                } else {
                    if (url.indexOf("mp.weixin.qq.com") != -1) {
                        wx.navigateTo({
                            url: '/pages/webview/webview?path=' + encodeURIComponent(url),
                        })
                    } else {
                        this.setData({
                            showtk: true
                        })
                        util.navContact(url, url)
                    }
                }
            } else {
                util.navContact(url, url)
            }
        }

    },

    // 外链进入客服会话
    contact() {
        this.setData({
            showtk: false,
        })
    },

    // 外链弹框关闭
    close() {
        this.setData({
            showtk: false,
        })
    },

    // 获取文章列表接口
    getwzList(id) {
        let infoData = {
            citycode: this.data.citycode == 'sz' ? 'bsy' : this.data.citycode,
            action: 'list',
            pagesize: 5,
            classid: id,

        };
        util.request('index.php', infoData, 'GET').then(res => {
            this.setData({
                wzList: res.data,
            })
        })
    },

    // 添加收藏接口
    addCollect(id) {
        let data = {
            action: "collect",
            option: "add",
            openid: wx.getStorageSync("openid"),
            citycode: this.data.citycode,
            value: JSON.stringify(this.data.collectValue),
            // value: this.data.collectValue,
            valueid: id,
            path: "article",
        };
        util.request('user.php', data, 'POST').then(res => {
            if (res.data.state == 1)
                wx.showToast({
                    title: res.data.message,
                })
            this.onShow();
        })
    },

    // 判断是否已收藏
    isCollect(id) {
        let data = {
            action: "collect",
            option: "list",
            openid: wx.getStorageSync("openid"),
            size: 10,
            page: 1,
            valueid: id,
            path: "article",
        };
        util.request('user.php', data, 'POST').then(res => {
            let collectFlag = res.data.state;
            let scData = res.data.data;
            this.setData({
                collectFlag,
                scData
            })
        })

    },

    // 我要办理
    banliTap(e) {
        let n = e.currentTarget.dataset.n;
        let m = e.currentTarget.dataset.m;
        banIndex[n] = m;
        let k = n + 1;
        if (conditions && conditions.length)
            if (k < conditions.length) {
                let conditionCurrent = conditions[k].content;
                for (let c = 0; c < conditionCurrent.length; c++) {
                    showorhide[k][c] = false;
                    for (let i = 0; i < condition.length; i++) {
                        let isFound = true;
                        for (let j = 0; j < banIndex.length; j++) {
                            if (j >= k) {
                                break;
                            }
                            if (condition[i]["condition"]["c" + j] != conditions[j]['content'][banIndex[j]]) {
                                isFound = false;
                                break;
                            }
                        }
                        if (isFound && condition[i]["condition"]["c" + k] == conditionCurrent[c]) {
                            showorhide[k][c] = true;
                            break;
                        }
                    }
                }
                showTap = [];
                for (let i = 0; i < showorhide.length; i++) {
                    if (showorhide[i].indexOf(true) != -1) {
                        showTap.push(true)
                    } else {
                        showTap.push(false)
                    }
                }
                var nextSelect = banIndex[n + 1];
                if (showorhide[n + 1][nextSelect] == false) {
                    let arr = [];
                    for (let i = 0; i < showorhide[n + 1].length; i++) {
                        if (showorhide[n + 1][i] == true) {
                            arr.push(i);
                            nextSelect = arr[0];
                        }
                    }
                    // nextSelect = 0;
                }
                var e = {
                    currentTarget: {
                        dataset: {
                            n: n + 1,
                            m: nextSelect
                        }
                    }
                };
                this.banliTap(e);
            } else {
                for (let i = 0; i < condition.length; i++) {
                    let isFound = true;
                    for (let j = 0; j < banIndex.length; j++) {
                        if (condition[i]['condition']['c' + j] != '') {
                            if (conditions[j])
                                if (condition[i]['condition']['c' + j] != conditions[j]['content'][banIndex[j]]) {
                                    isFound = false;
                                    break;
                                }
                        }
                    }

                    if (isFound) {
                        this.getInfoData(condition[i].bsyid);
                        break;
                    }
                }
            }
        this.setData({
            banIndex,
            showorhide,
            showTap
        })

    },

    // 文内链接点击事件
    link: function (e) {
        let id = e.currentTarget.dataset.id;
        let title = e.currentTarget.dataset.title;
        let url = e.currentTarget.dataset.url;
        let type = e.currentTarget.dataset.type;
        if (id != 0 && url == "") {
            wx.navigateTo({
                url: '/pages/article/detail/detail?id=' + id + '&type=' + type + '&city=' + this.data.citycode,
            });
        } else {
            if (url.indexOf("pages") == -1 && url.indexOf("mp.weixin.qq.com") == -1) {
                this.setData({
                    showtk: true
                })
                util.navContact(title, url)
            } else {
                util.navMethod(url);
            }
        }
    },

    // 收藏按钮
    collectBtn(e) {
        // let id = e.currentTarget.dataset.id;
        if (this.data.collectFlag != 1 && this.data.scData == "") {
            this.addCollect(this.data.id);
        }
        if (!wx.getStorageSync("collectCue")) {
            setTimeout(function () {
                wx.showModal({
                    content: '返回首页后，点击“我的”即可查看已收藏的内容。',
                    showCancel: false,
                    confirmText: "我知道了"
                })
            }, 2000)

        }
        wx.setStorageSync("collectCue", "collectCue")
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: this.data.bsyData ? this.data.bsyData.title : '',
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
            // path: 'pages/topicality/bsy/bsy?id=' + this.data.id + '&city=' + this.data.citycode,
        }
    },

    // 分享朋友圈
    onShareTimeline: function () {
        return {
            title: this.data.bsyData ? this.data.bsyData.title : '',
            imageUrl: "http://imgbdb3.bendibao.com/xcxbdb/file/20212/9/20210209161843_68677.jpg",
            query: {
                id: this.data.id,
                city: this.data.citycode
            },
        }
    },

    /**
     * 页面滑动事件的处理函数
     */
    onPageScroll: function () {
        this.setData({
            showNav: false,
        })
    }
})